/*
 * 文件名: MultiSelectDialog.java
 * 版    权：  Copyright PingAn Technology All Rights Reserved.
 * 描    述: [可以多选择的Dialog]
 * 创建人: XIAOFANQING001,ZOUGUOZHONG924
 * 创建时间: 2012-02-22
 * 
 * 修改人：
 * 修改时间:
 * 修改内容：[修改内容]
 */
package app.widget;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.longevityassistant.activity.R;

public class MultiSelectDialog extends BaseDialog {
	//供选择的对象列表
	private List<Bean> beans = new ArrayList<Bean>();
	// 返回已选择对象的接口
	private OnMultiSelectDialogSelectedListener onMultiSelectDialogSelectedListener;
	/**
	 * MultiSelectDialog的构造函数
	 * @param context 上下文
	 * @param currs 列表项名数组
	 */
	public MultiSelectDialog(Context context, String[] currs) {
		super(context);
		// 把currs数组的值传递给beans
		for (String s : currs) {
			beans.add(new Bean(s));
		}
		// 点击MultiSelectDialog的确定按钮返回已选项
		setBtn1ClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				List<String> strs = new ArrayList<String>();
				Bean b;
				for (int i = 0; i < beans.size(); i++) {
					b = beans.get(i);
					if (b.isChecked) {
						strs.add(b.curr);
					}
				}
				if (onMultiSelectDialogSelectedListener != null) {
					onMultiSelectDialogSelectedListener
							.onMultiSelectDialogSelected(strs);
				}
			}
		});

	}
	/**
	 * 初始化Dialog视图
	 */
	@Override
	public View createContentView() {
		View view = LayoutInflater.from(getContext()).inflate(
				R.layout.app_widget_multiselect_dialog, null);
		ListView lv = (ListView) view.findViewById(R.id.listview);

		final MyAdapter adapter = new MyAdapter(getContext(), -1, beans);

		lv.setAdapter(adapter);

		lv.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				Bean b = (Bean) parent.getItemAtPosition(position);
				if (b.isChecked) {
					b.isChecked = false;
				} else {
					b.isChecked = true;
				}
				adapter.notifyDataSetChanged();
			}
		});
		return view;
	}
	/**
	 * 初始化监听器
	 * @param onMultiSelectDialogSelectedListener
	 */
	public void setOnMultiSelectDialogSelectedListener(
			OnMultiSelectDialogSelectedListener onMultiSelectDialogSelectedListener) {
		this.onMultiSelectDialogSelectedListener = onMultiSelectDialogSelectedListener;
	}

	public OnMultiSelectDialogSelectedListener getOnMultiSelectDialogSelectedListener() {
		return onMultiSelectDialogSelectedListener;
	}
	/**
	 * MultiSelectDialog的适配器，展示或更新视图
	 */
	class MyAdapter extends ArrayAdapter<Bean> {
		ViewHolder holder;

		public MyAdapter(Context context, int textViewResourceId,
				List<Bean> objects) {
			super(context, textViewResourceId, objects);
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			if (convertView == null) {
				convertView = LayoutInflater.from(getContext()).inflate(
						R.layout.app_widget_dialog_multiple_select_item, null);
				holder = new ViewHolder();
				holder.tv = (TextView) convertView
						.findViewById(R.id.multi_textView);
				holder.iv = (ImageView) convertView
						.findViewById(R.id.multi_imageView);
				convertView.setTag(holder);

			} else {
				holder = (ViewHolder) convertView.getTag();
			}

			Bean b = getItem(position);
			holder.tv.setText(b.curr);
			if (b.isChecked) {
				holder.iv.setBackgroundResource(R.drawable.app_widget_radio_box_checked);
			} else {
				holder.iv.setBackgroundResource(R.drawable.app_widget_radio_box);
			}
			return convertView;

		}

		class ViewHolder {
			TextView tv;
			ImageView iv;
		}
	}
	/**
	 * 内部的bean类
	 * 用于值的传递
	 */
	private static class Bean {
		public Bean(String curr) {
			this.curr = curr;
		}
		//选择项的名称
		String curr;
		//是否被选中
		boolean isChecked;

	}
	/**
	 * 用于返回已选数据的监听接口
	 */
	public interface OnMultiSelectDialogSelectedListener {
		public void onMultiSelectDialogSelected(List<String> selectedList);
	}
}
